package com.helloworld.demo.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockTest4 {



    public static void main(String[] args) throws InterruptedException {
        Lock lock = new ReentrantLock(true);

        for(int i = 0;i < 500;i++){
            new Thread(()->{

                lock.lock();

                try{
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    System.out.println(Thread.currentThread().getName() + "running... ");
                }finally {
                    lock.unlock();
                }

            },"Thread-" + i).start();
        }

        Thread.sleep(1000);

        for(int i = 0; i < 500;i++){
            new Thread(()->{
                lock.lock();
                try{
                    System.out.println("                                        " + Thread.currentThread().getName()+"running");
                }finally {
                    lock.unlock();
                }
            },"强行插入"+i).start();
        }
    }

}
